package com.example.springboot02_controller.controller.controller;

import com.example.springboot02_controller.controller.entity.User;
import com.example.springboot02_controller.controller.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/test")

public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/hello")//测试
    public String hello(){
        return "hello world";
    }


    @RequestMapping("/getById")
    //查询
    public String getById(Integer id){
        User user = userService.getById(id);
        return  user.toString();
    }

    @ResponseBody
    @RequestMapping(value="/add",method = RequestMethod.POST)
    //添加
    public String add(@RequestBody User user){
        User newuser = userService.addUser(user);
        user.setId(newuser.getId());
        return newuser.toString();
    }

    @ResponseBody
    @RequestMapping(value="update",method = RequestMethod.POST)
    //修改
    public String update(@RequestBody User user){
        User newuser = userService.updateUser(user);
        user.setId(newuser.getId());
        return newuser.toString();
    }

    @RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
    //删除
    public String delete(@PathVariable int id){
        userService.deleteUser(id);
        return "sucesss";
    }
}



